﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MINERP.MODEL.NewPhotoRindGrid;

using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Data.SqlClient;
using MINERP.COM;

namespace MINERP.DAL.NewPhotoRindGrid
{
    /// <summary>
    /// 对产品信息的数据操作
    /// </summary>
    public class OrderInfoDAL_Grid
    {
        /// <summary>
        /// 根据orderProductId得到分片需要的产品信息
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <param name="oInfo_Grid"></param>
        /// <returns></returns>
        public int GetOrderInfo_Grid(int orderProductId, out OrderInfo_Grid oInfo_Grid)
        {
            int result = 0;//代表成功
            oInfo_Grid = null;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetOrder_GridInfoInfoByOrderProductId");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);

            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        oInfo_Grid = new OrderInfo_Grid() { OrderProductId=orderProductId, ShopName = dr.GetString(0), OrderTime = dr.GetDateTime(1), FetchTime = dr.GetDateTime(2), OrderProductName = dr.GetString(3), CtmName = dr.GetString(4), OrderPriority = dr.GetInt32(5), ParentProductId = dr.GetInt32(6),Remark=dr.GetString(7), IsPrintThumbnail=dr.GetBoolean(8) };
                    }
                }
            }
            catch (SqlException)
            {

                result = 2;//从数据库数取数据时出错
            }
            finally
            {
                if (dbCommand.Connection.State!=ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }
            if (oInfo_Grid==null)
            {
                result = 3;
            }

            return result;
        }

        public int GetLastTechIdByOrderProductId(int orderProductId, out int curTechId, out OrderFlowListStatus status)
        {
            int result = 1;
            curTechId = 0;
            status = 0;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetLastTechStatus");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);
            try
            {
                using (IDataReader dr = db.ExecuteReader(dbCommand))
                {
                    while (dr.Read())
                    {
                        curTechId = dr.GetInt32(0);
                        status = (OrderFlowListStatus)dr.GetInt32(1);
                    }
                }
            }
            catch (SqlException)
            {

                result = -2;//从数据库数取数据时出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result;
        }

        /// <summary>
        /// 检查产品的照片是否全处于某种状态
        /// </summary>
        /// <param name="orderProductId"></param>
        /// <param name="photoStatus"></param>
        /// <returns></returns>
        public int CheckOrdersPhotoIsAssignCompleted(int orderProductId, int photoStatus)
        {
            int result = 1;

            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("GetPhotosCountByOrderProductIDandPhotoStatus");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);
            db.AddInParameter(dbCommand, "photoStatus", DbType.Int32, photoStatus);
            try
            {
                result = (int)db.ExecuteScalar(dbCommand);
            }
            catch (SqlException)
            {

                result = -2;//从数据库数取数据时出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result; 
        }

        //完成产品的分片结点,包括返单与正常单，并得到下一个流程节点的名称
        public int OrverOrderProductFlowTech(int orderProductId,bool isReBackOrder,int Uid,out string nextTechName)
        {
            int result = 1;
            nextTechName = "";
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("OverAssignPhotoTechId");
            db.AddInParameter(dbCommand, "orderProductId", DbType.Int32, orderProductId);
            db.AddInParameter(dbCommand, "isReBackProduct", DbType.Int32, isReBackOrder);
            db.AddInParameter(dbCommand, "uid", DbType.Int32, Uid);
            try
            {
                nextTechName = (string)db.ExecuteScalar(dbCommand);
            }
            catch (SqlException)
            {

                result = -2;//从数据库数取数据时出错
            }
            finally
            {
                if (dbCommand.Connection.State != ConnectionState.Closed)
                {
                    dbCommand.Connection.Close();
                }
            }

            return result; 
        }
    }
}
